package com.xtsoft.platform.base.system.controller;

import com.fasterxml.jackson.databind.JsonMappingException;

import com.xtsoft.core.controller.GenericController;

import com.xtsoft.core.repository.QueryFilter;
import com.xtsoft.core.repository.QueryResult;
import com.xtsoft.core.utils.ResultMsg;
import com.xtsoft.core.utils.StringUtils;
import com.xtsoft.platform.base.system.domain.SysDictionary;
import com.xtsoft.platform.base.system.service.SysDictionaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParseException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;

 /**
 * 
 * @ClassName:  SysDictionary   
 * <p> 数据字典处理类 </p>
 * @author: Codegen-代码生成器
 * @date:   2019年12月3日 上午10:59:46
 */ 
@Tag(name = "数据字典--处理接口")
@RestController
public class SysDictionaryController extends GenericController{
	
	private final Logger log = LoggerFactory.getLogger(SysDictionaryController.class);
	
	@Autowired	
	private SysDictionaryService sysDictionaryService;
	
	
    /**
     *  <p>新增一个数据字典缺陷</p>  
     * @param SysDictionary 要新增的数据字典
     * @return ResponseEntity<ResultMsg>, ResultMsg的Data中包含 数据字典
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常                     
	 * @author Codegen-代码生成器	
	 * @date 2020-07-16 12:10:57
    */	
	
	@Parameter(name = "SysDictionary", description = "要新增的数据字典", required = false)
	@Operation(summary ="新增，根据前台传递的数据字典新增")
    @PostMapping("/sysDictionary/add")
    public ResponseEntity<ResultMsg> add(@RequestBody SysDictionary SysDictionary) throws URISyntaxException {
		if(null == SysDictionary){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"新增数据字典失败，传递的数据字典为空"),HttpStatus.OK); 
		}
		log.info("新增数据字典缺陷开始......");
        if (!StringUtils.isEmpty(SysDictionary.getDictId())) {
		    log.info("新增数据字典缺陷失败，新数据字典不能具有SysDictionaryId属性值");
            return new ResponseEntity<ResultMsg>(new ResultMsg(false,"新增数据字典失败，新数据字典不能具有SysDictionaryId属性值"),HttpStatus.OK);
        }
        SysDictionary result = sysDictionaryService.save(SysDictionary);
		log.info("新增数据字典成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"新增成功",result),HttpStatus.OK);
    }	

    /**
     * <p>更新一个数据字典</p>    
     * @param SysDictionary 要更新的数据字典
     * @return ResponseEntity<ResultMsg>, json格式
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
     */
	@Parameter(name = "SysDictionary", description = "要更新的数据字典", required = false)
	@Operation(summary ="更新，根据传递的 数据字典更新实体")
    @PostMapping("/sysDictionary/update")

    public ResponseEntity<ResultMsg> update(@RequestBody SysDictionary SysDictionary) throws URISyntaxException {
		if(null == SysDictionary){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"更新数据字典失败，传递的数据字典为空"),HttpStatus.OK); 
		}
		log.info("单条更新数据字典开始......");
		//若 数据字典无主键值，说明调用错误
        if (StringUtils.isBlank(SysDictionary.getDictId())) {
			log.info("单条更新数据字典失败，数据字典对象SysDictionaryId无属性值");
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"更新失败，数据字典对象SysDictionaryId无属性值"),HttpStatus.OK);
        }
        SysDictionary result = sysDictionaryService.save(SysDictionary);
		log.info("单条更新数据字典成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"更新成功",result),HttpStatus.OK);
    }  

    /**
	 *   <p>查询数据字典列表，若分页，则页码信息在Request的参数中，包括当前页，每页记录数</p>
     * @return ResponseEntity<QueryResult<SysDictionary>>，json格式
     * @throws URISyntaxException 如果出错，则产生页码信息的 HTTP headers
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
     */
	
	@Operation(summary ="查询，根据传递的条件查询数据字典列表，条件参数在Request中")
    @GetMapping("/sysDictionary/getAll")
 
	
    public ResponseEntity<ResultMsg> getAll()
        throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
		log.info("获取数据字典列表开始......");
        QueryFilter<SysDictionary> filter=new QueryFilter<SysDictionary>(SysDictionary.class);
        filter.addFilterFromRequest(request);
        QueryResult<SysDictionary> result= sysDictionaryService.anyQuery(filter);
		if(null == result || null == result.getResultList() || result.getResultList().size() < 0){
			log.info("获取数据字典列表失败");
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取数据字典列表失败"),HttpStatus.OK);
		}
		log.info("获取数据字典列表成功");	
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"获取列表成功",result.getResultList()),HttpStatus.OK);
    }
    /**
	 *   <p>查询数据字典列表，若分页，则页码信息在Request的参数中，包括当前页，每页记录数</p>
     * @return ResponseEntity<QueryResult<SysDictionary>>，json格式
     * @throws URISyntaxException 如果出错，则产生页码信息的 HTTP headers
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
     */
	
	@Operation(summary ="查询，根据传递的条件查询数据字典列表，条件参数在Request中")
    @GetMapping("/sysDictionary/getAllTwo") 
	
    public ResponseEntity<ResultMsg> getAllTwo()
        throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
		log.info("获取数据字典列表开始......");
        QueryFilter<SysDictionary> filter=new QueryFilter<SysDictionary>(SysDictionary.class);
        filter.addFilterFromRequest(request);
        QueryResult<SysDictionary> result= sysDictionaryService.anyQuery(filter);
		if(null == result || null == result.getResultList() || result.getResultList().size() < 0){
			log.info("获取数据字典列表失败");
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取数据字典列表失败"),HttpStatus.OK);
		}
		SysDictionary sysDictionary=new SysDictionary();
		sysDictionary.setDictId("000");
		sysDictionary.setNodeId("0000");
		sysDictionary.setParaName("主字典");
		sysDictionary.setParentNodeId("00");
		result.getResultList().add(sysDictionary);
		log.info("获取数据字典列表成功");	
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"获取列表成功",result.getResultList()),HttpStatus.OK);
    }
    /**
	 *   <p>根据数据字典唯一ID值获取数据字典</p>
     * @param id 数据字典唯一ID值
     * @return  ResponseEntity<SysDictionary>
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57		
     */
	
	@Parameter(name = "id", description="对象唯一Id", required = false)
	@Operation(summary ="获取单个数据字典，若根据数据字典唯一Id获取到了多个对象则发生异常")
    @GetMapping("/sysDictionary/get/{id}")
	
    public ResponseEntity<ResultMsg> get(@PathVariable String id) {
		if(StringUtils.isBlank(id)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取单个数据字典失败，未给定数据字典的唯一标识"),HttpStatus.OK);
		}
		log.info("获取数据字典开始......");
        SysDictionary SysDictionary = sysDictionaryService.get(id);
		/*
		*  <p> ofNullable执行逻辑：如果对象为空，则执行orElse，否则执行.map...
		*		return Optional.ofNullable(sysRole).map(result -> new ResponseEntity<>(result, HttpStatus.OK))
		*		.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));		
		*/
		
		if( null == SysDictionary ){
			log.info("获取数据字典失败");	
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取单个数据字典失败，给定的数据字典可能被删除"),HttpStatus.OK);
		}
		log.info("获取数据字典成功");
		return new ResponseEntity<ResultMsg>(new ResultMsg(true,"获取成功",SysDictionary),HttpStatus.OK);
    } 

    /**
     *     <p>根据数据字典唯一ID值删除单个数据字典</p>
     * @param  id 数据字典唯一ID值
     * @return ResponseEntity,json格式
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57		 
     */
	@Parameter(name = "id", description="数据字典唯一Id", required = false)
    @GetMapping("/sysDictionary/delete/{id}")
	@Operation(summary ="删除单个数据字典") 
    public ResponseEntity<ResultMsg> delete(@PathVariable String id) {
		if(StringUtils.isBlank(id)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"删除单个数据字典失败，未给定数据字典的唯一标识"),HttpStatus.OK);
		}
		log.info("删除单条数据字典开始......");
		sysDictionaryService.remove(id);
		log.info("删除单条数据字典成功");		
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"删除成功"),HttpStatus.OK);
    }
    
}
